{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "3.10. 多层感知机的简洁实现.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/chongzicbo/Dive-into-Deep-Learning-tf.keras/blob/master/3.10.%20%E5%A4%9A%E5%B1%82%E6%84%9F%E7%9F%A5%E6%9C%BA%E7%9A%84%E7%AE%80%E6%B4%81%E5%AE%9E%E7%8E%B0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qq7xMwNDLTci",
        "colab_type": "text"
      },
      "source": [
        "##3.10. 多层感知机的简洁实现\n",
        "下面我们使用tf.keras来实现上一节中的多层感知机。首先导入所需的包或模块。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mVkQ84gRC_AY",
        "colab_type": "code",
        "outputId": "d3464456-8b74-4ec1-91e8-adaed5346d33",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 64
        }
      },
      "source": [
        "%matplotlib inline\n",
        "import tensorflow as tf\n",
        "import matplotlib.pyplot as plt\n",
        "from IPython import  display\n",
        "from tensorflow import keras\n",
        "import tensorflow.data as tfdata\n",
        "import numpy as np"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "<p style=\"color: red;\">\n",
              "The default version of TensorFlow in Colab will soon switch to TensorFlow 2.x.<br>\n",
              "We recommend you <a href=\"https://www.tensorflow.org/guide/migrate\" target=\"_blank\">upgrade</a> now \n",
              "or ensure your notebook will continue to use TensorFlow 1.x via the <code>%tensorflow_version 1.x</code> magic:\n",
              "<a href=\"https://colab.research.google.com/notebooks/tensorflow_version.ipynb\" target=\"_blank\">more info</a>.</p>\n"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yCrtS1F1DaiH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "tf.enable_eager_execution()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XTW04iOeLdBl",
        "colab_type": "text"
      },
      "source": [
        "###3.10.1. 定义模型\n",
        "和softmax回归唯一的不同在于，我们多加了一个全连接层作为隐藏层。它的隐藏单元个数为256，并使用ReLU函数作为激活函数。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pXv3EgI2DdnF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net=keras.Sequential()\n",
        "net.add(keras.layers.Dense(256,activation='relu'))\n",
        "net.add(keras.layers.Dense(10))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FrfGV5lXLjM0",
        "colab_type": "text"
      },
      "source": [
        "###3.10.2. 读取数据并训练模型\n",
        "我们使用与“softmax回归的简洁实现”一节中训练softmax回归几乎相同的步骤来读取数据并训练模型。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "R4qjtFzaDrGW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "batch_size=256\n",
        "buffer_size=10000\n",
        "def load_data_fashion_mnist(batch_size,buffer_size):\n",
        "  (x_train,y_train),(x_test,y_test)=keras.datasets.fashion_mnist.load_data()\n",
        "  x_train=np.reshape(x_train,[x_train.shape[0],-1])#将三维张量reshape成二维\n",
        "  x_test=np.reshape(x_test,[x_test.shape[0],-1])\n",
        "  train_iter=tfdata.Dataset.from_tensor_slices((x_train,y_train)).map(lambda x,y:(x/255,y)).shuffle(buffer_size).batch(batch_size)\n",
        "  test_iter=tfdata.Dataset.from_tensor_slices((x_test,y_test)).map(lambda x,y:(x/255,y)).batch(batch_size)\n",
        "  return train_iter,test_iter"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "eGNKqNlZD_uP",
        "colab_type": "code",
        "outputId": "d715f6f2-63eb-436b-86d9-4447735a2a66",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 155
        }
      },
      "source": [
        "train_iter,test_iter=load_data_fashion_mnist(batch_size=batch_size,buffer_size=buffer_size)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n",
            "32768/29515 [=================================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n",
            "26427392/26421880 [==============================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n",
            "8192/5148 [===============================================] - 0s 0us/step\n",
            "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n",
            "4423680/4422102 [==============================] - 0s 0us/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sNNnp11MEJAW",
        "colab_type": "code",
        "outputId": "6101d2a6-a9ca-4d93-aaa9-db94a7b63af7",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 294
        }
      },
      "source": [
        "loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True)\n",
        "optimizer=keras.optimizers.SGD(learning_rate=0.5)\n",
        "num_epochs=5\n",
        "net.compile(optimizer=optimizer,loss=loss,metrics=['accuracy'])\n",
        "net.fit_generator(train_iter,validation_data=test_iter,epochs=num_epochs)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch 1/5\n",
            "232/235 [============================>.] - ETA: 0s - loss: 0.4705 - acc: 0.8265Epoch 1/5\n",
            "235/235 [==============================] - 8s 36ms/step - loss: 0.4696 - acc: 0.8268 - val_loss: 0.5054 - val_acc: 0.8003\n",
            "Epoch 2/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 0.4104 - acc: 0.8483Epoch 1/5\n",
            "235/235 [==============================] - 8s 36ms/step - loss: 0.4103 - acc: 0.8483 - val_loss: 0.4894 - val_acc: 0.8131\n",
            "Epoch 3/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 0.3775 - acc: 0.8608Epoch 1/5\n",
            "235/235 [==============================] - 8s 36ms/step - loss: 0.3774 - acc: 0.8608 - val_loss: 0.4536 - val_acc: 0.8259\n",
            "Epoch 4/5\n",
            "232/235 [============================>.] - ETA: 0s - loss: 0.3571 - acc: 0.8682Epoch 1/5\n",
            "235/235 [==============================] - 8s 36ms/step - loss: 0.3565 - acc: 0.8684 - val_loss: 0.4421 - val_acc: 0.8328\n",
            "Epoch 5/5\n",
            "234/235 [============================>.] - ETA: 0s - loss: 0.3399 - acc: 0.8749Epoch 1/5\n",
            "235/235 [==============================] - 9s 37ms/step - loss: 0.3398 - acc: 0.8749 - val_loss: 0.4435 - val_acc: 0.8342\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tensorflow.python.keras.callbacks.History at 0x7f12cefa29e8>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "scG37QhQEw43",
        "colab_type": "text"
      },
      "source": [
        "###3.10.3. 小结\n",
        "* 通过keras可以更简洁地实现多层感知机。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lNXOduSeLycf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}